package cn.bocweb.spinning.view.forun;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.SocietyBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

public class ForunSocialFragment extends Fragment {

    ForunSocialAdapter forunSocialAdapter;
    List<SocietyBean.DataBean> list;
    UserPresenter presenter;
    LoadingDialog mLoadingDialog;
    int page = 1;
    String userid;

    @Bind(R.id.forun_social_list)
    ListView forunSocialList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_forun_social, container, false);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);




        list = new ArrayList<>();
        presenter = new UserPresenterImp();
        mLoadingDialog = new LoadingDialog(getActivity());
//        mLoadingDialog.show();
        forunSocialAdapter = new ForunSocialAdapter(getActivity(), list);
        initListView();

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(getActivity());
        userid = dataEntity.getUserid();

        presenter.society(page, userid);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStop() {
        if (mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onStop();
    }

    public void onEventMainThread(SocietyBean bean) {
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            forunSocialAdapter.notifyDataSetChanged();
        }
    }

    private void initListView() {
        forunSocialList.setAdapter(forunSocialAdapter);
        forunSocialList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ForunSocialActivity.class);
                intent.putExtra(Constant.TO_FORUM_SOCIETY, list.get(position).getId());
                intent.putExtra(Constant.TO_FORUM_SOCIETY_COLOR, position);
                intent.putExtra(Constant.TO_FORUM_SOCIETY_TITLE, list.get(position).getTitle());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
